package com.prd.tools;

import com.baomidou.mybatisplus.extension.service.IService;
import com.prd.dto.*;
import com.prd.service.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DTOListUtil {
    public static <T, E extends IService> void compareDBAndDTOList(List<T> inDBList, List<T> inDTOList, E service, Map<String, Object> otherParam) {
        List<T> deleteList = new ArrayList<>();
        if (inDBList == null) {
            inDBList = new ArrayList<>();
        }
        if (inDTOList == null) {
            inDTOList = new ArrayList<>();
        }
        inDTOList.forEach(item -> {
            if (item instanceof CustomerGuestDTO) {
                CustomerGuestDTO guestDTO = (CustomerGuestDTO)item;
                guestDTO.setCustomerNo((String) otherParam.get("customerNo"));
                guestDTO.setGuestNo(String.valueOf(guestDTO.hashCode()));
            }
            else if (item instanceof CustomerProjectDTO) {
                CustomerProjectDTO projectDTO = (CustomerProjectDTO)item;
                projectDTO.setProjectId(String.valueOf(projectDTO.hashCode()));
            }
            else if (item instanceof CustomerRemarkDTO) {
                CustomerRemarkDTO remarkDTO = (CustomerRemarkDTO)item;
                String no = (String) otherParam.get("customerNo");
                if (no == null)
                    no = (String) otherParam.get("guestNo");
                remarkDTO.setNo(no);
            }
            else if (item instanceof CustomerContactDTO) {
                CustomerContactDTO contactDTO = (CustomerContactDTO)item;
                String no = (String) otherParam.get("customerNo");
                if (no == null)
                    no = (String) otherParam.get("guestNo");
                contactDTO.setNo(no);
            }
            else if (item instanceof CustomerCommRecordDTO) {
                CustomerCommRecordDTO recordDTO = (CustomerCommRecordDTO) item;
                String no = (String) otherParam.get("customerNo");
                if (no == null)
                    no = (String) otherParam.get("guestNo");
                recordDTO.setNo(no);
            }
        });
        List<T> finalInDTOList = inDTOList;
        deleteList = inDBList.stream()
                .filter(s -> !finalInDTOList.contains(s))
                .collect(Collectors.toList());

//        List<T> addList = inDTOList.stream()
//                .filter(s -> !inDBList.contains(s))
//                .collect(Collectors.toList());

        deleteList.forEach(item -> {
            if (item instanceof CustomerGuestDTO) {
                CustomerGuestDTO guestDTO = (CustomerGuestDTO)item;
                ((CustomerGuestService)service).uRemoveCustomer((String) otherParam.get("customerNo"), guestDTO.getGuestNo());
            }
            else if (item instanceof CustomerProjectDTO) {
                CustomerProjectDTO projectDTO = (CustomerProjectDTO)item;
                ((CustomerProjectService)service).removeByProjectId(projectDTO.getProjectId());
            }
            else if (item instanceof CustomerRemarkDTO) {
                CustomerRemarkDTO remarkDTO = (CustomerRemarkDTO)item;
                ((CustomerRemarkService)service).removeById(remarkDTO.getId());
            }
            else if (item instanceof CustomerContactDTO) {
                CustomerContactDTO contactDTO = (CustomerContactDTO)item;
                ((CustomerContactService)service).removeById(contactDTO.getId());
            }
            else if (item instanceof CustomerCommRecordDTO) {
                CustomerCommRecordDTO recordDTO = (CustomerCommRecordDTO) item;
                ((CustomerCommRecordService)service).removeById(recordDTO.getId());
            }
        });

        inDTOList.forEach(item -> {
            if (item instanceof CustomerGuestDTO) {
                CustomerGuestDTO guestDTO = (CustomerGuestDTO)item;
                guestDTO.setCustomerNo((String) otherParam.get("customerNo"));
                ((CustomerGuestService)service).uUpdateAndAdd(guestDTO);
            }
            else if (item instanceof CustomerProjectDTO) {
                CustomerProjectDTO projectDTO = ((CustomerProjectDTO) item);
                projectDTO.setCustomerNo((String) otherParam.get("customerNo"));
                ((CustomerProjectService)service).uUpdateAndAddSimple(projectDTO);
            }
            else if (item instanceof CustomerRemarkDTO) {
                CustomerRemarkDTO remarkDTO = (CustomerRemarkDTO) item;
                remarkDTO.setType((int) otherParam.get("code"));
                remarkDTO.setNo((String) otherParam.get("customerNo"));
                ((CustomerRemarkService)service).uUpdateAndAdd(remarkDTO);
            }
            else if (item instanceof CustomerContactDTO) {
                CustomerContactDTO contactDTO = (CustomerContactDTO) item;
                contactDTO.setType((int) otherParam.get("code"));
                contactDTO.setNo((String) otherParam.get("customerNo"));
                ((CustomerContactService)service).uUpdateAndAdd(contactDTO);
            }
            else if (item instanceof CustomerCommRecordDTO) {
                CustomerCommRecordDTO recordDTO = (CustomerCommRecordDTO) item;
                recordDTO.setNo((String) otherParam.get("customerNo"));
                ((CustomerCommRecordService)service).uUpdateAndAdd(recordDTO);
            }
        });
    }
}
